package org.fastsyncer.framework.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.fastsyncer.common.constant.CommonConstant;
import org.fastsyncer.common.util.ApplicationUtil;
import org.fastsyncer.common.util.CryptoUtils;
import org.fastsyncer.common.util.PropertiesUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import fastsyncer.config.ConfigFactory;

/**
 * @ClassName: ConfigService 
 * @Description: 系统参数配置
 * @author: YangPei
 * @date: 2017年8月2日 下午5:20:27
 */
public class ConfigService {

    private static final ConfigService instance = new ConfigService();

    private ConfigService() {
    }

    public static ConfigService getInstance() {
        return instance;
    }

    /**
     * @Title: getSystemParams 
     * @Description: 获取系统参数
     * @throws Exception
     * @return: List<String>
     */
    public HashMap<String, String> getSystemParams() throws Exception {
        // 获取系统参数
        HashMap<String, String> hashMap = new HashMap<String, String>();
        List<String> list = ConfigFactory.getInstance().get();
        if (list != null && !list.isEmpty()) {
            for (String p : list) {
                hashMap.put(p, ApplicationUtil.getKey(p));
            }
        }
        return hashMap;
    }

    /**
     * 
     * @Title: updateSystemParams 
     * @Description: 修改系统参数
     * @param params
     * @throws Exception
     * @return: String
     */
    public String updateSystemParams(String params) throws Exception {
        JSONObject pStr = new JSONObject(params);
        if (null != pStr) {
            // 获取参数个数
            JSONArray names = pStr.names();
            int nLen = names.length();
            if (nLen < 1) {
                throw new Exception("系统参数为空!");
            }

            Map<String, String> map = new HashMap<String, String>();
            for (int i = 0; i < nLen; i++) {
                String pName = names.getString(i);
                String pVal = pStr.getString(pName);
                if (StringUtils.isBlank(pVal)) {
                    continue;
                }

                // 设置参数名，参数值
                map.put(pName, pVal);
            }

            // 保存参数
            ConfigFactory.getInstance().update(map);
        }
        return "系统参数修改成功!";
    }

    /**
     * 
     * @Title: updateSystemPwd 
     * @Description: 修改管理员密码
     * @param pwd 密码
     * @param pwd 新密码
     * @throws Exception
     * @return: String
     */
    public String updateAdminPwd(String pwd, String newPwd) throws Exception {
        //检查密码是否正确
        String rpwd = ApplicationUtil.getKey(CommonConstant.CONFIG_ADMIN_PWD);
        // 解密
        String decodeBASE64 = CryptoUtils.decodeBASE64(rpwd);
        if (!decodeBASE64.equals(pwd)) {
            throw new IllegalArgumentException("密码错误.");
        }
        String encodeBASE64 = CryptoUtils.encodeBASE64(newPwd);
        ApplicationUtil.setKey(CommonConstant.CONFIG_ADMIN_PWD, encodeBASE64);

        // 持久化到本地磁盘
        PropertiesUtil.update(ApplicationUtil.getKey(CommonConstant.CONFIG_PATH_APPLICATION_PROPERTIES), 
                CommonConstant.CONFIG_ADMIN_PWD, encodeBASE64);
        return "个人密码修改成功!";
    }

}
